package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.mapper.CheckGroupMapper;
import com.itheima.pojo.CheckGroup;
import com.itheima.service.CheckGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service(interfaceClass = CheckGroupService.class)
public class CheckGroupServiceImpl implements CheckGroupService {


    @Autowired
    private CheckGroupMapper checkIGroupMapper;

    @Override
    @Transactional
    public void add(Integer[] checkitemIds, CheckGroup checkGroup) {

        //添加组信息
        checkIGroupMapper.add(checkGroup);
        //设置组与项关系

        if (checkitemIds!=null&&checkitemIds.length>0){
            for (Integer checkitemId : checkitemIds) {
                checkIGroupMapper.addCheckGroupAndCheckItem(checkitemId,checkGroup.getId());
            }
        }

    }

    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {

        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());

        Page<CheckGroup> page = checkIGroupMapper.selectByCondition(queryPageBean.getQueryString());

        return new PageResult(page.getTotal(),page.getResult());


    }

    @Override
    public CheckGroup findById(int id) {
      CheckGroup list= checkIGroupMapper.findById(id);
      return list;
    }

    @Override
    public void edit(Integer[] checkitemIds, CheckGroup checkGroup) {
        checkIGroupMapper.updateCheckGroup(checkGroup);
        //删除中间关系
        checkIGroupMapper.delAssociation(checkGroup.getId());
        //设置中间关系
        if (checkitemIds!=null&&checkitemIds.length!=0) {
            for (Integer checkitemId : checkitemIds) {
                checkIGroupMapper.setAssociation(checkGroup.getId(), checkitemId);
            }
        }
    }

    @Override
    public List<CheckGroup> findAllGroup() {
        List<CheckGroup> list=checkIGroupMapper.findAllGroup();
        return list;
    }

    @Override
    public void del(int id) {
        //删除中间关系
        checkIGroupMapper.delAssociation(id);
        checkIGroupMapper.delGroup(id);
    }
}
